<?php

class Log {
	/**
	 * log file path
	 * @var unknown_type
	 */
	protected $_logPath = null;
	/**
	 * log file filename
	 * @var unknown_type
	 */
	protected $_logFile = null;
	/**
	 * log file path is set to start again
	 * @var unknown_type
	 */
	public $_logEnd  = false;
	/**
	 * log namespace
	 * @var string
	 */
	protected $_namespace;
	/**
	 * log record object
	 * @var obj
	 */
	protected $_logObj = null;
	
	/**
	 * Initialization log configuration and set the namespace
	 * @param namespace $namespace
	 */
	public function __construct($namespace = null){
		if (!empty($namespace)){
			$this->setNamespace($namespace);
		}
		$this->init();
	}
	/**
	 * When the instance disappears close links to resources
	 */
	function __destruct(){
		$this->closeFileLog();
	}
	/**
	 * Initialization method
	 * @return Default_Service_Log
	 */
	public function init(){
		$this->_logEnd = false;
		$this->_logPath = null;
		$this->_logFile = null;
		$this->addDir(Yii::app()->basePath.'/log');
		$this->addDir($this->_namespace);
		return $this;
	}
	/**
	 * Set log namespace
	 * @param namespace $namespace
	 * @return Default_Service_Log
	 */
	public function setNamespace($namespace){
		$this->_namespace = $namespace;
		$this->init();
		return $this;
	}
	/**
	 * Gets the namespace
	 * @return string
	 */
	public function getNamespace(){
		return $this->_namespace;
	}
	/**
	 * Add a log folder hierarchy
	 * @param dirName $dirName
	 * @return Default_Service_Log
	 */
	public function addDir($dirName){
		if ($this->_logEnd){
			$this->init();
		}
		$this->_logPath = $this->_logPath.$dirName.'/';
		return $this;
	}
	/**
	 * Add log information
	 * @param logInfo $info
	 * @param logInfoType $type
	 * @return Default_Service_Log
	 */
	public function addLog($info,$type = 'INFO'){
		$infoMapper = array(
				'<[LOGTYPE]>'	=> 	$type,
				'<[LOGINFO]>' 	=>	is_array($info)?json_encode($info):$info,
				'<[LOGTIME]>'	=> 	time(),
				'<[LOGDATE]>'	=>	date('Y-m-d H:i:s'),
		);
		fwrite($this->_logObj, $this->logTemplate($infoMapper));
		return $this;
	}
	/**
	 * Set log file name
	 * @param logFileName $fileName
	 * @return Default_Service_Log
	 */
	public function setFile($fileName){
		$this->_logFile = $fileName;
		$this->log();
		return $this;
	}
	/**
	 * Open the log file resource and mark the end of the log file
	 * @return Default_Service_Log
	 */
	public function log(){
		$this->fileLog();
		$this->_logEnd = true;
		return $this;
	}
	/**
	 * Open the file resources if the file path does not exist on the establishment of a folder
	 * @return Default_Service_Log
	 */
	protected function fileLog(){
		if (!file_exists($this->_logPath) || !is_dir($this->_logPath)) {
			mkdir($this->_logPath,0777,true);
		}
		$this->_logObj = fopen($this->_logPath.$this->_logFile.".log","a");
		return $this;
	}
	/**
	 * Close the file resources
	 */
	protected function closeFileLog(){
		fclose($this->_logObj);
	}
	/**
	 * log message template
	 * @param array $info
	 * @return mixed
	 */
	protected function logTemplate($info = array()){
		$logTemplate = <<<LogInfo
{[<[LOGTYPE]>] TIME:<[LOGTIME]> DATE:<[LOGDATE]> } <[LOGINFO]>
LogInfo;
		$logInfo = str_replace(array_keys($info), $info, $logTemplate."\n");
		return $logInfo;
	}
}

?>